using UnityEngine;
using UnityEngine.UI;

using Photon.Pun;
using Photon.Realtime;

using System.Collections;

public class PlayerNameInputField : MonoBehaviour
{

    const string playerNamePrefKey = "PlayerName";  //保存PlayerPref键以避免拼写错误


    private void Start()    //在PlayerPrefs中获取玩家上次游玩时的名字
    {
        string defaultName = string.Empty;
        InputField _inputField = this.GetComponent<InputField>();
        if (_inputField != null)
        {
            if (PlayerPrefs.HasKey(playerNamePrefKey))
            {
                defaultName = PlayerPrefs.GetString(playerNamePrefKey);
                _inputField.text = defaultName;
            }
        }

        PhotonNetwork.NickName = defaultName;
    }

    /// <summary>
    /// 设置玩家的名字，并将其保存在PlayerPrefs中以备将来使用。
    /// </summary>
    /// <param name="value"></param>
    public void SetPlayerName(string value)
    {
        // #Important
        if (string.IsNullOrEmpty(value))
        {
            Debug.LogError("Player Name is null or empty");
            return;
        }
        PhotonNetwork.NickName = value;


        PlayerPrefs.SetString(playerNamePrefKey, value);
    }
}
